﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace weba.Sales
{
    public partial class StockOutNoteHeaderList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                //UART.BLL.Sales.StockOutNoteHeader bll = new UART.BLL.Sales.StockOutNoteHeader();
                //设置经办人
                Accounts_Users currentUser = mk.l.Accounts_Users.FirstOrDefault();
                //设置根部门ID
                var bllDept = mk.l.Accounts_Department.ToList();
                this.lblRootDepartmentID.Text = bllDept.Where(j => j.ClassID == (int.Parse(currentUser.DepartmentID))).FirstOrDefault().RootID.ToString();
                this.lblStrWhere.Text = " Status!='1' and RootDepartmentID=" + this.lblRootDepartmentID.Text;
              //  this.AspNetPager.RecordCount = bll.GetCount(this.lblStrWhere.Text);
                this.BindData();
                //设置分页页数
                this.txtPageSize.Attributes.Add("onkeydown", "javascript:SetFocusPageSize();");

            }
        }
        private void BindData()
        {
         var bll = mk.l.Sales_StockOutNoteHeader.ToList();
         var ds = bll.ToList();
            this.myGridView.DataSource = ds;
            this.myGridView.DataBind();
        }

        /// <summary>
        /// 分页绑定数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager_PageChanged(object sender, EventArgs e)
        {
            this.BindData();
        }

        /// <summary>
        /// Handles the Click event of the btnSetPageSize control.设置分页页数
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnSetPageSize_Click(object sender, EventArgs e)
        {
           
        }

        /// <summary>
        /// 绑定数据时显示状态信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
              var bllOrderHeader =mk.l.Sales_SalesOrderHeader.ToList();
                       Sales_SalesOrderHeader mOrderHeader = bllOrderHeader.Where(h=>h.SalesOrderID==int.Parse(this.myGridView.DataKeys[e.Row.RowIndex].Values["SalesOrderID"].ToString())).FirstOrDefault();

                //显示客户类型
                Label lblCustomerName = (Label)(e.Row.FindControl("lblCustomerName"));
              var bllC = mk.l.Sales_Customer.ToList();
Sales_Customer mC = bllC.Where(h=>h.CustomerID==mOrderHeader.CustomerID).FirstOrDefault();
                if (mC != null)
                {
                    lblCustomerName.Text = mC.CustomerName;
                }
                //显示设计师姓名
                Label lblDesigner = (Label)(e.Row.FindControl("lblDesigner"));
        var bllUser =mk.l.Accounts_Users.ToList();
              Accounts_Users mUser = bllUser.Where(h=>h.UserName==mOrderHeader.Designer).FirstOrDefault();
                lblDesigner.Text = mUser.TrueName;
                //显示出库单经办人姓名
                Label lblManager = (Label)(e.Row.FindControl("lblManager"));
                mUser = bllUser.Where(h=>h.UserName==this.myGridView.DataKeys[e.Row.RowIndex].Values["Manager"].ToString()).FirstOrDefault();
                lblManager.Text = mUser.TrueName;

                //显示状态
                Label lblStatus = (Label)(e.Row.FindControl("lblStatus"));
                string strStatus = this.myGridView.DataKeys[e.Row.RowIndex].Values["Status"].ToString();
                switch (strStatus)
                {
                    case "1":
                        lblStatus.Text = "初始添加";
                        lblStatus.ForeColor = System.Drawing.Color.Red;
                        break;
                    case "2":
                        lblStatus.Text = "已提交";
                        lblStatus.ForeColor = System.Drawing.Color.Red;
                        //linkEdit.Visible = false;
                        break;
                    case "3":
                        lblStatus.Text = "已出库";
                        lblStatus.ForeColor = System.Drawing.Color.Green;
                        break;
                    default:
                        lblStatus.Text = "未知";
                        break;
                }

                //让数字以文本形式表示
                //e.Row.Cells[0].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
                //e.Row.Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
                //e.Row.Cells[3].Attributes.Add("style", "vnd.ms-excel.numberformat:@");

            }
        }

        /// <summary>
        /// Handles the Click event of the btnSearch control.查询相关订单信息
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.lblStrWhere.Text = " Status!='1' and RootDepartmentID=" + this.lblRootDepartmentID.Text;
            //设置查询条件
            //所有的条件都加and 最后再去掉就行了 tnnd早想到这个办法多好啊 不用写那么多if了
            StringBuilder strWhere = new StringBuilder();
            if (this.txtCustomerName.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and CustomerName like '%{0}%'", mk.InputText(this.txtCustomerName.Text, 50));
            }
            if (this.txtPhone.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and Phone like '%{0}%'", mk.InputText(this.txtPhone.Text, 20));
            }
            if (this.txtCellPhone.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and CellPhone like '%{0}%'", mk.InputText(this.txtCellPhone.Text, 20));
            }
            if (this.txtAddress.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and Address like '%{0}%'", mk.InputText(this.txtAddress.Text, 100));
            }

            StringBuilder strWhere2 = new StringBuilder();


            if (this.txtSalesOrderNumber.Text.Trim() != "")
            {
                strWhere2.AppendFormat(" and StockOutNoteNumber like '%{0}%'", mk.InputText(this.txtSalesOrderNumber.Text, 30));
            }

            StringBuilder strWhere3 = new StringBuilder();
            if (strWhere2.Length > 0)
            {
                strWhere3.Append(strWhere2);
            }
            if (strWhere.Length > 0)
            {
                strWhere3.Append(" and SalesOrderID in (select SalesOrderID from Sales_SalesOrderHeader where CustomerID in (select CustomerID from Sales_Customer where ");
                strWhere3.Append(strWhere.ToString().Substring(5, strWhere.Length - 5));
                strWhere3.Append("))");
            }
            if (strWhere3.Length > 0)
            {
                //this.lblStrWhere.Text = strWhere3.ToString().Substring(5, strWhere3.Length - 5);
                this.lblStrWhere.Text += strWhere3.ToString();
            }
            //UART.BLL.Sales.StockOutNoteHeader bll = new UART.BLL.Sales.StockOutNoteHeader();
            //this.AspNetPager.RecordCount = bll.GetCount(this.lblStrWhere.Text);
            this.BindData();
        }



    }
}
